package com.example.restatr.Atcivity.Movie;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;


import com.bumptech.glide.Glide;
import com.example.restatr.Atcivity.Movie.Bean.MovieFilmBean;
import com.example.restatr.Atcivity.Movie.Bean.MovieSeatBean;
import com.example.restatr.Atcivity.Movie.Bean.MovieTheatreTimesBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class ChooseSeatAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private TextView movieName;
    private TextView date;
    private TextView movieHall;
    private ImageView cover;
    private RecyclerView seat;
    private RecyclerView selectSeat;
    private CardView btn;
    private TextView btnText;
    private MovieTheatreTimesBean.RowsBean 影院影片场次信息;
    private List<MovieSeatBean.DataBean> movieSeatBeans = new ArrayList<>();//座位信息
    private List<MovieSeatBean.DataBean> selectMovieSeatBeans = new ArrayList<>();//选择的座位信息
    private selectSeatAdapter selectSeatAdapter;
    private int FaresPrice;
    private MovieFilmBean.RowsBean 电影信息;
    private seatAdapter seatAdapter = new seatAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_seat);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        movieName = findViewById(R.id.movieName);
        date = findViewById(R.id.date);
        movieHall = findViewById(R.id.movieHall);
        seat = findViewById(R.id.seat);
        selectSeat = findViewById(R.id.select_seat);
        btn = findViewById(R.id.btn);
        btnText = findViewById(R.id.btnText);
        cover = findViewById(R.id.cover);
        Intent intent = getIntent();
        影院影片场次信息 = new Gson().fromJson(intent.getStringExtra("影院影片场次信息"), new TypeToken<MovieTheatreTimesBean.RowsBean>() {
        }.getType());
        电影信息 = new Gson().fromJson(intent.getStringExtra("电影信息"), new TypeToken<MovieFilmBean.RowsBean>() {
        }.getType());
        Glide.with(this)
                .load(Tools.ip + 电影信息.getCover())
                .into(cover);
        Tools.SetBarTitle(tb, barTitle, 影院影片场次信息.getTheatreName(), search, this, false);
        movieName.setText(影院影片场次信息.getMovieName());
        date.setText(影院影片场次信息.getPlayDate() + " " + 影院影片场次信息.getStartTime() + "-" + 影院影片场次信息.getEndTime());
        movieHall.setText(影院影片场次信息.getRoomName());
        selectSeat.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        selectSeatAdapter = new selectSeatAdapter();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectMovieSeatBeans.size() > 0) {
                    Intent orderIntent = new Intent(ChooseSeatAt.this, MovieOrderAt.class);
                    orderIntent.putExtra("座位信息", new Gson().toJson(selectMovieSeatBeans));
                    orderIntent.putExtra("影院影片场次信息", new Gson().toJson(影院影片场次信息));
                    orderIntent.putExtra("电影信息", intent.getStringExtra("电影信息"));
                    orderIntent.putExtra("总价", FaresPrice);
                    orderIntent.putExtra("影院名称", barTitle.getText().toString());
                    startActivity(orderIntent);
                }
            }
        });
        GetSeatInfo();
    }

    private void GetSeatInfo() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/movie/theatre/list4times?id=" + 影院影片场次信息.getId() + "&roomId=" + 影院影片场次信息.getRoomId(), "");
            MovieSeatBean movieSeatBean = new Gson().fromJson(get, MovieSeatBean.class);
            movieSeatBeans.addAll(movieSeatBean.getData());
            runOnUiThread(() -> {
                if (movieSeatBeans.size() > 0) {
                    seat.setLayoutManager(new GridLayoutManager(ChooseSeatAt.this, 16));
                    seat.setAdapter(seatAdapter);
                }
            });
        }).start();
    }

    class seatAdapter extends RecyclerView.Adapter<seatAdapter.SeatViewHolder> {

        @NonNull
        @Override
        public SeatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.seat_item_layout, parent, false);
            return new SeatViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull SeatViewHolder holder, int position) {
            holder.seat.setEnabled(movieSeatBeans.get(position).isSeatStatus());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ResourceType")
                @Override
                public void onClick(View v) {
                    if (holder.seat.isEnabled()) {
                        movieSeatBeans.get(position).setSeatStatus(false);
                        selectMovieSeatBeans.remove(movieSeatBeans.get(position));
                        selectSeatAdapter.notifyDataSetChanged();
                        FaresPrice -= 影院影片场次信息.getPrice();
                    } else {
                        movieSeatBeans.get(position).setSeatStatus(true);
                        movieSeatBeans.get(position).setPos(position);//设置位置的下标，后面删除所选位置需要用到
                        selectMovieSeatBeans.add(movieSeatBeans.get(position));
                        selectSeat.setAdapter(selectSeatAdapter);
                        FaresPrice += 影院影片场次信息.getPrice();
                    }
                    btnText.setText(FaresPrice + "元 确定选座");
                    seatAdapter.notifyItemChanged(position);

                }
            });
        }

        @Override
        public int getItemCount() {
            return movieSeatBeans.size();
        }

        class SeatViewHolder extends RecyclerView.ViewHolder {
            private ImageView seat;

            public SeatViewHolder(@NonNull View itemView) {
                super(itemView);
                seat = itemView.findViewById(R.id.seat);
            }
        }
    }

    class selectSeatAdapter extends RecyclerView.Adapter<selectSeatAdapter.selectSeatViewHolder> {

        @NonNull
        @Override
        public selectSeatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.select_seat_item, parent, false);
            return new selectSeatViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull selectSeatViewHolder holder, int position) {
            holder.money.setText("￥" + 影院影片场次信息.getPrice());
            holder.seat.setText(selectMovieSeatBeans.get(position).getRow() + "排" + selectMovieSeatBeans.get(position).getCol() + "座");
            holder.clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    movieSeatBeans.get(selectMovieSeatBeans.get(position).getPos()).setSeatStatus(false);
                    seatAdapter.notifyItemChanged(selectMovieSeatBeans.get(position).getPos());
                    selectMovieSeatBeans.remove(position);
                    selectSeatAdapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return selectMovieSeatBeans.size();
        }

        class selectSeatViewHolder extends RecyclerView.ViewHolder {
            private TextView seat;
            private TextView money;
            private CardView clear;

            public selectSeatViewHolder(@NonNull View itemView) {
                super(itemView);
                seat = itemView.findViewById(R.id.seat);
                money = itemView.findViewById(R.id.money);
                clear = itemView.findViewById(R.id.clear);
            }
        }
    }
}